package com.neusoft.elmboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.neusoft.elmboot.Dto.DeliveryaddressDto;
import com.neusoft.elmboot.Vo.DeliveryaddressVo;
import com.neusoft.elmboot.common.result.Result;
import com.neusoft.elmboot.po.DeliveryaddressEntity;
import com.neusoft.elmboot.service.DeliveryaddressService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/deliverys")
@Slf4j
public class DeliveryaddressController {
    @Autowired
    private DeliveryaddressService deliveryaddressService;

    /**
     * 通过userId获取地址
     * @param userId
     * @return
     */
    @ApiOperation("通过userId获取地址")
    @GetMapping("/listAddressByUserId")
    public Result<List<DeliveryaddressVo>> listDeliveryAddressByUserId(Long userId){
        LambdaQueryWrapper<DeliveryaddressEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DeliveryaddressEntity::getUserId,userId);
        List<DeliveryaddressEntity> deliveryaddressEntityList = deliveryaddressService.list(lqw);
        List<DeliveryaddressVo> deliveryaddressVoList = deliveryaddressEntityList.stream().map((item) -> {
            DeliveryaddressVo deliveryaddressVo = new DeliveryaddressVo();
            BeanUtils.copyProperties(item, deliveryaddressVo);
            return deliveryaddressVo;
        }).collect(Collectors.toList());

        return Result.success(deliveryaddressVoList);

    }

    /**
     * 新增地址
     * @param deliveryaddressVo
     * @return
     */
    @ApiOperation("新增地址")
    @PostMapping
    public Result saveAddress(@RequestBody DeliveryaddressVo deliveryaddressVo){
        log.info("新增地址");
        DeliveryaddressEntity deliveryaddressEntity = new DeliveryaddressEntity();
        BeanUtils.copyProperties(deliveryaddressVo, deliveryaddressEntity);
        deliveryaddressService.save(deliveryaddressEntity);

        return Result.success();

    }

    /**
     * 回显地址
     * @param daId
     * @return
     */
    @GetMapping("/getDeliveryAddressById")
    public Result<DeliveryaddressVo> getById(Long daId){
        log.info("回显地址");
        DeliveryaddressEntity deliveryaddressEntity = deliveryaddressService.getById(daId);
        DeliveryaddressVo deliveryaddressVo = new DeliveryaddressVo();
        BeanUtils.copyProperties(deliveryaddressEntity, deliveryaddressVo);
        return Result.success(deliveryaddressVo);
    }

    /**
     * 编辑地址
     * @param deliveryaddressDto
     * @return
     */
    @ApiOperation("编辑地址")
    @PutMapping
    public Result updateDeliveryAddress(@RequestBody DeliveryaddressDto deliveryaddressDto){
        log.info("编辑地址");
        DeliveryaddressEntity deliveryaddressEntity = new DeliveryaddressEntity();
        BeanUtils.copyProperties(deliveryaddressDto, deliveryaddressEntity);
        deliveryaddressService.updateById(deliveryaddressEntity);
        return Result.success();
    }

}
